package com.rm.zeyou.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rm.zeyou.entity.Notice;
import com.rm.zeyou.mapper.NoticeMapper;
import com.rm.zeyou.response.ApiRestResponse;
import com.rm.zeyou.service.NoticeService;
import com.rm.zeyou.util.check.CheckEmptyUtil;
import com.rm.zeyou.vo.request.NoticeListVo;
import com.rm.zeyou.vo.response.NoticeListResponseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 公告ID 服务实现类
 * </p>
 *
 * @author YanYang
 * @since 2025-04-07 10:56:04
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public ApiRestResponse<String> indexNotice() {
        return ApiRestResponse.success(noticeMapper.selectOne(new LambdaQueryWrapper<Notice>().orderByDesc(Notice::getCreateTime).last(" limit 1")).getNoticeTitle());
    }

    @Override
    public ApiRestResponse<PageInfo<NoticeListResponseVo>> noticeList(Long userId, NoticeListVo requestVo) {
        PageHelper.startPage(requestVo.getPageNum(), requestVo.getPageSize());
        List<Notice> notices = noticeMapper.selectList(new LambdaQueryWrapper<Notice>().orderByDesc(Notice::getCreateTime));

        List<NoticeListResponseVo> responseVoList = new ArrayList<>();

        if (!CheckEmptyUtil.isEmpty(notices)) {
            PageInfo<Notice> source = new PageInfo<>(notices);
            PageInfo<NoticeListResponseVo> target = new PageInfo<>();
            BeanUtils.copyProperties(source, target);
            for (Notice notice : source.getList()) {
                NoticeListResponseVo vo = new NoticeListResponseVo();
                vo.setId(notice.getId());
                vo.setNoticeTitle(notice.getNoticeTitle());
                vo.setNoticeContent(notice.getNoticeContent());
                vo.setCreateTime(notice.getCreateTime());
                int i = noticeMapper.selUserIsRead(userId, notice.getId());
                if (i > 0) {
                    vo.setIsRead(1);
                } else {
                    vo.setIsRead(0);
                }

                responseVoList.add(vo);

            }
            target.setList(responseVoList);
            return ApiRestResponse.success(target);
        }
        return ApiRestResponse.success(new PageInfo<>());
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public ApiRestResponse<Integer> readNotice(Long userId, Long noticeId) {
        Notice notice = noticeMapper.selectById(noticeId);
        Integer i = 0;
        if (StrUtil.isEmpty(notice.getUserId())) {
            i = noticeMapper.updateOneNotice(userId, noticeId);
        } else {
            if (!notice.getUserId().contains(String.valueOf(userId))){
                i = noticeMapper.updateNotice(userId, noticeId);
            }
        }
        return ApiRestResponse.success(i);
    }
}
